home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP04 / DEVCAPS1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  5.0 KB  |  138 lines

  1. /*---------------------------------------------------------
  2.    DEVCAPS1.C -- Device Capabilities Display Program No. 1
  3.                  (c) Charles Petzold, 1996
  4.   ---------------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <string.h>
  8.  
  9. #define NUMLINES ((int) (sizeof devcaps / sizeof devcaps [0]))
  10.  
  11. struct
  12.      {
  13.      int  iIndex ;
  14.      char *szLabel ;
  15.      char *szDesc ;
  16.      }
  17.      devcaps [] =
  18.           {
  19.           HORZSIZE,      "HORZSIZE",     "Width in millimeters:",
  20.           VERTSIZE,      "VERTSIZE",     "Height in millimeters:",
  21.           HORZRES,       "HORZRES",      "Width in pixels:",
  22.           VERTRES,       "VERTRES",      "Height in raster lines:",
  23.           BITSPIXEL,     "BITSPIXEL",    "Color bits per pixel:",
  24.           PLANES,        "PLANES",       "Number of color planes:",
  25.           NUMBRUSHES,    "NUMBRUSHES",   "Number of device brushes:",
  26.           NUMPENS,       "NUMPENS",      "Number of device pens:",
  27.           NUMMARKERS,    "NUMMARKERS",   "Number of device markers:",
  28.           NUMFONTS,      "NUMFONTS",     "Number of device fonts:",
  29.           NUMCOLORS,     "NUMCOLORS",    "Number of device colors:",
  30.           PDEVICESIZE,   "PDEVICESIZE",  "Size of device structure:",
  31.           ASPECTX,       "ASPECTX",      "Relative width of pixel:",
  32.           ASPECTY,       "ASPECTY",      "Relative height of pixel:",
  33.           ASPECTXY,      "ASPECTXY",     "Relative diagonal of pixel:",
  34.           LOGPIXELSX,    "LOGPIXELSX",   "Horizontal dots per inch:",
  35.           LOGPIXELSY,    "LOGPIXELSY",   "Vertical dots per inch:",
  36.           SIZEPALETTE,   "SIZEPALETTE",  "Number of palette entries:",
  37.           NUMRESERVED,   "NUMRESERVED",  "Reserved palette entries:",
  38.           COLORRES,      "COLORRES",     "Actual color resolution:"
  39.           } ;
  40.  
  41. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  42.  
  43. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  44.                     PSTR szCmdLine, int iCmdShow)
  45.      {
  46.      static char szAppName[] = "DevCaps1" ;
  47.      HWND        hwnd ;
  48.      MSG         msg ;
  49.      WNDCLASSEX  wndclass ;
  50.  
  51.      wndclass.cbSize        = sizeof (wndclass) ;
  52.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  53.      wndclass.lpfnWndProc   = WndProc ;
  54.      wndclass.cbClsExtra    = 0 ;
  55.      wndclass.cbWndExtra    = 0 ;
  56.      wndclass.hInstance     = hInstance ;
  57.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  58.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  59.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  60.      wndclass.lpszMenuName  = NULL ;
  61.      wndclass.lpszClassName = szAppName ;
  62.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  63.  
  64.      RegisterClassEx (&wndclass) ;
  65.  
  66.      hwnd = CreateWindow (szAppName, "Device Capabilities",
  67.                           WS_OVERLAPPEDWINDOW,
  68.                           CW_USEDEFAULT, CW_USEDEFAULT,
  69.                           CW_USEDEFAULT, CW_USEDEFAULT,
  70.                           NULL, NULL, hInstance, NULL) ;
  71.  
  72.      ShowWindow (hwnd, iCmdShow) ;
  73.      UpdateWindow (hwnd) ;
  74.  
  75.      while (GetMessage (&msg, NULL, 0, 0))
  76.           {
  77.           TranslateMessage (&msg) ;
  78.           DispatchMessage (&msg) ;
  79.           }
  80.      return msg.wParam ;
  81.      }
  82.  
  83. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  84.      {
  85.      static int  cxChar, cxCaps, cyChar ;
  86.      char        szBuffer[10] ;
  87.      HDC         hdc ;
  88.      int         i ;
  89.      PAINTSTRUCT ps ;
  90.      TEXTMETRIC  tm ;
  91.  
  92.      switch (iMsg)
  93.           {
  94.           case WM_CREATE:
  95.                hdc = GetDC (hwnd) ;
  96.  
  97.                GetTextMetrics (hdc, &tm) ;
  98.                cxChar = tm.tmAveCharWidth ;
  99.                cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ;
  100.                cyChar = tm.tmHeight + tm.tmExternalLeading ;
  101.  
  102.                ReleaseDC (hwnd, hdc) ;
  103.                return 0 ;
  104.  
  105.           case WM_PAINT:
  106.                hdc = BeginPaint (hwnd, &ps) ;
  107.  
  108.                for (i = 0 ; i < NUMLINES ; i++)
  109.                     {
  110.                     TextOut (hdc, cxChar, cyChar * (1 + i),
  111.                              devcaps[i].szLabel,
  112.                              strlen (devcaps[i].szLabel)) ;
  113.  
  114.                     TextOut (hdc, cxChar + 22 * cxCaps, cyChar * (1 + i),
  115.                              devcaps[i].szDesc,
  116.                              strlen (devcaps[i].szDesc)) ;
  117.  
  118.                     SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
  119.  
  120.                     TextOut (hdc, cxChar + 22 * cxCaps + 40 * cxChar,
  121.                              cyChar * (1 + i), szBuffer,
  122.                              wsprintf (szBuffer, "%5d",
  123.                                   GetDeviceCaps (hdc, devcaps[i].iIndex))) ;
  124.  
  125.                     SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
  126.                     }
  127.  
  128.                EndPaint (hwnd, &ps) ;
  129.                return 0 ;
  130.  
  131.           case WM_DESTROY:
  132.                PostQuitMessage (0) ;
  133.                return 0 ;
  134.           }
  135.  
  136.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  137.      }
  138.